package com.pug.service;

import com.pug.mapper.hotel.HotelZanMapper;
import com.pug.pojo.HotelommentUserZan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Author: lqm
 * create: 2022/5/13 19:09
 * Lover: lqx
 *
 * @see
 */
@Service
public class TimeService {
    @Resource
    HotelZanMapper mapper;
    @Resource
    RedisTemplate<String,Integer> integerRedisTemplate;
    @Scheduled(cron = "0/60 * * * * ? ")
    public void saveZansToMysql(){
        //将redis的数据存入mysql
        Set<Object> comentId = integerRedisTemplate.opsForHash().keys("comment");//全部hash的名称
        for(Object s:comentId){
//            integerRedisTemplate.opsForHash().put("tempComment",s,1);//新的hash名称
            Set<Object> keys = integerRedisTemplate.opsForHash().keys(s + "");
            for(Object key:keys){
//                Object o = integerRedisTemplate.opsForHash().get(s + "", key + "");
                HotelommentUserZan hotelommentUserZan = new HotelommentUserZan();
                hotelommentUserZan.setCommentId(new Long((String) s));
                hotelommentUserZan.setUserid(new Long( key+""));
                mapper.insert(hotelommentUserZan);
                integerRedisTemplate.opsForHash().delete(s+"",key+"");
            }
            integerRedisTemplate.opsForHash().delete("comment",s+"");
        }
    }
}
